import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import './pages/page_login.dart';
import './pages/page_regist.dart';
import './pages/page_lobby.dart';
import './widget/scaffold_page.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './user_proof.dart';
import './widget/loading_widget.dart';

void main() {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _StateMyApp createState() {
    return _StateMyApp();
  }
}
class _StateMyApp extends State<MyApp> {
  bool inited = false;
  void initUserData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    this.inited = true;
    UserProof.initSharedPreferences(prefs);
    
    this.setState((){});
  }
  _StateMyApp() {
    this.initUserData();
  }

  @override
  Widget build(BuildContext context)  {
    const fontTextStyle = TextStyle(fontFamily: '阿开漫画体', fontSize: 17);
    return MaterialApp(
      title: '卡牌游戏',
      theme: ThemeData(
        buttonTheme: ButtonThemeData(
          minWidth: 0,
          height: 0,
          padding: EdgeInsets.all(0),
          buttonColor: Colors.transparent
        ),
        textTheme: TextTheme(
          display4: fontTextStyle,
          display3: fontTextStyle,
          display2: fontTextStyle,
          display1: fontTextStyle,
          headline: fontTextStyle,
          title: fontTextStyle,
          subhead: fontTextStyle,
          body2: fontTextStyle,
          body1: fontTextStyle,
          caption: fontTextStyle,
          button: fontTextStyle,
          subtitle: fontTextStyle,
          overline: fontTextStyle,
        )
      ),
      home: ScaffoldPage(child: inited ? null == UserProof.userId ? PageLogin() : PageLobby(isSortLogin: true) :LoadingWidget()),
      routes: <String, WidgetBuilder> {
        '/login': (BuildContext context) => ScaffoldPage(child: PageLogin()),
        '/regist': (BuildContext context) => ScaffoldPage(child: PageRegist()),
        '/lobby': (BuildContext context) => ScaffoldPage(child: PageLobby()),
      },
    );
  }
}